clear all
set more off
cap log close


********************************************************************************
***** Project: The Short and Long Term Effects of In-Person Performance Feedback
********************************************************************************
***** A. R. Soetevent & G. J. Romensen
********************************************************************************
***** Feedback Messages Incidence and Composition
********************************************************************************
***** Latest update: 18-01-2020
********************************************************************************
*global filepath "C:\JPEMicReplication"
log using "$filepath/Logs/07Feedbackincidencecomposition.log", replace
*** 1. Retrieve treatment status

import delimited "$filepath\Data\Randomisatie\RandomisatieFinal.csv"

rename rugnr chauf_nr_rug

sort chauf_nr_rug
		
save "$filepath\Data\Using databases\randomization.dta", replace
clear

*** 2. Check for each feedback month whether a report is constructed.

local i=0

while `i'<=11 {
import excel "$filepath\Data\Using databases\feedback_`i'.xlsx", firstrow
keep if total_score!=.
levelsof driver_id, local(yesfeed_`i')
global yesfeed_`i' `yesfeed_`i''
clear
local i=`i'+1
}

use "$filepath\Data\Using databases\Overallfeedback.dta"

local i=0

while `i'<=11 {
gen yesfeed_`i'=0
foreach x of global yesfeed_`i' {
replace yesfeed_`i'=1 if chauf_nr_rug==`x'
label variable  yesfeed_`i' "Driver received a feedback report on his/her performance in month `i'"
}
local i=`i'+1
}

*** 3. Merge with treatment status

sort chauf_nr_rug
joinby chauf_nr_rug using "$filepath\Data\Using databases\randomization.dta", unmatched(master)
drop _merge

order chauf_nr_rug treatment

*** 4. Feedback incidence and composition

rename commentaar_in_december commentaar_in_0
rename commentaar_in_januari commentaar_in_1
rename commentaar_in_februari commentaar_in_2
rename commentaar_in_maart commentaar_in_3
rename commentaar_in_april commentaar_in_4
rename commentaar_in_mei commentaar_in_5
rename commentaar_in_juni commentaar_in_6
rename commentaar_in_juli commentaar_in_7
rename commentaar_in_augustus commentaar_in_8
rename commentaar_in_september commentaar_in_9
rename commentaar_in_oktober commentaar_in_10

*****************************************************************************
***  Incidence and Composition of Targeted Peer-Comparison Messages
*****************************************************************************

* T1 (1n/0p)
local i=0

while `i'<=10 {
gen feedback_1n0p_`i'=cond(strpos(commentaar_in_`i', "Beste"),1,0) if treatment=="T2" & yesfeed_`i'==1
qui sum feedback_1n0p_`i'
local share_1n0p_`i'=r(mean)
di `i'
di `share_1n0p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
gen acceleratie_`i'=cond(strpos(commentaar_in_`i', "accelereren"),1,0) if treatment=="T2" & yesfeed_`i'==1 &  feedback_1n0p_`i'==1 
gen bochten_`i'=cond(strpos(commentaar_in_`i', "bochten"),1,0) if treatment=="T2" & yesfeed_`i'==1 & feedback_1n0p_`i'==1  
gen remmen_`i'=cond(strpos(commentaar_in_`i', "remmen"),1,0) if treatment=="T2" & yesfeed_`i'==1 & feedback_1n0p_`i'==1 

gen ACCmsgNEG_`i'=acceleratie_`i'
gen BOCHTmsgNEG_`i'=bochten_`i'
gen REMmsgNEG_`i'=remmen_`i'
label variable ACCmsgNEG_`i' "Driver received BOTTOM-50 message on Acceleration in month `i'"
label variable BOCHTmsgNEG_`i' "Driver received BOTTOM-50 message on Cornering in month `i'"
label variable REMmsgNEG_`i' "Driver received BOTTOM-50 message on Braking in month `i'"

qui sum acceleratie_`i'
local acceleratie_1n0p_`i'=r(mean)
qui sum bochten_`i'
local bochten_1n0p_`i'=r(mean)
qui sum remmen_`i'
local remmen_1n0p_`i'=r(mean)
di `i'
di `acceleratie_1n0p_`i''
di `remmen_1n0p_`i''
di `bochten_1n0p_`i''
local i=`i'+1
}

drop feedback_1n0p_* acceleratie_* bochten_* remmen_* 

* T2 (1n/1p)
local i=0

while `i'<=10 {
gen notemptycheck_`i'=cond(strpos(commentaar_in_`i', "Beste"),1,0) if treatment=="T3" & yesfeed_`i'==1
gen feedback_1n1p_0n0p_`i'=cond(notemptycheck_`i'==0,1,0) if treatment=="T3" & yesfeed_`i'==1
qui sum feedback_1n1p_0n0p_`i'
local share_1n1p_0n0p_`i'=r(mean)
di `i'
di `share_1n1p_0n0p_`i''
local i=`i'+1
}

drop feedback_1n1p_0n0p_*

local i=0
while `i'<=10 {
gen positivecheck_`i'=cond(strpos(commentaar_in_`i', "beste 25 procent"),1,0) if treatment=="T3" & yesfeed_`i'==1
gen negativecheck_`i'=cond(strpos(commentaar_in_`i', "onderste 50 procent"),1,0) if treatment=="T3" & yesfeed_`i'==1
gen feedback_1n1p_1n0p_`i'=cond(positivecheck_`i'==0 & notemptycheck_`i'==1,1,0) if treatment=="T3" & yesfeed_`i'==1
gen feedback_1n1p_0n1p_`i'=cond(negativecheck_`i'==0 & notemptycheck_`i'==1,1,0) if treatment=="T3" & yesfeed_`i'==1
gen feedback_1n1p_1n1p_`i'=cond(positivecheck_`i'==1 & negativecheck_`i'==1 & notemptycheck_`i'==1,1,0) if treatment=="T3" & yesfeed_`i'==1
qui sum feedback_1n1p_1n0p_`i'
local share_1n1p_1n0p_`i'=r(mean)
qui sum feedback_1n1p_0n1p_`i'
local share_1n1p_0n1p_`i'=r(mean)
qui sum feedback_1n1p_1n1p_`i'
local share_1n1p_1n1p_`i'=r(mean)
di `i'
di `share_1n1p_1n0p_`i''
di `share_1n1p_0n1p_`i''
di `share_1n1p_1n1p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
gen acceleratie_`i'=cond(strpos(commentaar_in_`i', "accelereren"),1,0) if treatment=="T3" & yesfeed_`i'==1 &  feedback_1n1p_1n0p_`i'==1 
gen bochten_`i'=cond(strpos(commentaar_in_`i', "bochten"),1,0) if treatment=="T3" & yesfeed_`i'==1 & feedback_1n1p_1n0p_`i'==1  
gen remmen_`i'=cond(strpos(commentaar_in_`i', "remmen"),1,0) if treatment=="T3" & yesfeed_`i'==1 & feedback_1n1p_1n0p_`i'==1 


replace ACCmsgNEG_`i'=cond(strpos(commentaar_in_`i', "accelereren behoort u tot de onderste"),1,0) if treatment=="T3" & yesfeed_`i'==1 
replace BOCHTmsgNEG_`i'=cond(strpos(commentaar_in_`i', "bochten nemen behoort u tot de onderste"),1,0) if treatment=="T3" & yesfeed_`i'==1 
replace REMmsgNEG_`i'=cond(strpos(commentaar_in_`i', "remmen behoort u tot de onderste"),1,0) if treatment=="T3" & yesfeed_`i'==1 

label variable ACCmsgNEG_`i' "Driver received BOTTOM-50 message on Acceleration in month `i'"
label variable BOCHTmsgNEG_`i' "Driver received BOTTOM-50 message on Cornering in month `i'"
label variable REMmsgNEG_`i' "Driver received BOTTOM-50 message on Braking in month `i'"


qui sum acceleratie_`i'
local acceleratie_1n1p_1n0p_`i'=r(mean)
qui sum bochten_`i'
local bochten_1n1p_1n0p_`i'=r(mean)
qui sum remmen_`i'
local remmen_1n1p_1n0p_`i'=r(mean)
di `i'
di `acceleratie_1n1p_1n0p_`i''
di `remmen_1n1p_1n0p_`i''
di `bochten_1n1p_1n0p_`i''
local i=`i'+1
}

drop feedback_1n1p_1n0p_* acceleratie_* bochten_* remmen_* 

local i=0
while `i'<=10 {
gen acceleratie_`i'=cond(strpos(commentaar_in_`i', "accelereren"),1,0) if treatment=="T3" & yesfeed_`i'==1 &  feedback_1n1p_0n1p_`i'==1 
gen bochten_`i'=cond(strpos(commentaar_in_`i', "bochten"),1,0) if treatment=="T3" & yesfeed_`i'==1 & feedback_1n1p_0n1p_`i'==1  
gen remmen_`i'=cond(strpos(commentaar_in_`i', "remmen"),1,0) if treatment=="T3" & yesfeed_`i'==1 & feedback_1n1p_0n1p_`i'==1 

gen ACCmsgPOS_`i'=cond(strpos(commentaar_in_`i', "accelereren behoort u tot de beste"),1,0) if treatment=="T3" & yesfeed_`i'==1 
gen BOCHTmsgPOS_`i'=cond(strpos(commentaar_in_`i', "bochten nemen behoort u tot de beste"),1,0) if treatment=="T3" & yesfeed_`i'==1 
gen REMmsgPOS_`i'=cond(strpos(commentaar_in_`i', "remmen behoort u tot de beste"),1,0) if treatment=="T3" & yesfeed_`i'==1 

label variable ACCmsgPOS_`i' "Driver received TOP-25 message on Acceleration in month `i'"
label variable BOCHTmsgPOS_`i' "Driver received TOP-25 message on Cornering in month `i'"
label variable REMmsgPOS_`i' "Driver received TOP-25 message on Braking in month `i'"


qui sum acceleratie_`i'
local acceleratie_1n1p_0n1p_`i'=r(mean)
qui sum remmen_`i'
local remmen_1n1p_0n1p_`i'=r(mean)
qui sum bochten_`i'
local bochten_1n1p_0n1p_`i'=r(mean)
di `i'
di `acceleratie_1n1p_0n1p_`i''
di `remmen_1n1p_0n1p_`i''
di `bochten_1n1p_0n1p_`i''
local i=`i'+1
}

drop feedback_1n1p_0n1p_* acceleratie_* bochten_* remmen_* 

local i=0

while `i'<=10 {
gen acceleratie_`i'=cond(strpos(commentaar_in_`i', "accelereren"),1,0) if treatment=="T3" & yesfeed_`i'==1 &  feedback_1n1p_1n1p_`i'==1 
gen bochten_`i'=cond(strpos(commentaar_in_`i', "bochten"),1,0) if treatment=="T3" & yesfeed_`i'==1 & feedback_1n1p_1n1p_`i'==1  
gen remmen_`i'=cond(strpos(commentaar_in_`i', "remmen"),1,0) if treatment=="T3" & yesfeed_`i'==1 & feedback_1n1p_1n1p_`i'==1 
qui sum acceleratie_`i'
local acceleratie_1n1p_1n1p_`i'=r(mean)
qui sum remmen_`i'
local remmen_1n1p_1n1p_`i'=r(mean)
qui sum bochten_`i'
local bochten_1n1p_1n1p_`i'=r(mean)
di `i'
di `acceleratie_1n1p_1n1p_`i''
di `remmen_1n1p_1n1p_`i''
di `bochten_1n1p_1n1p_`i''
local i=`i'+1
}

drop feedback_1n1p_1n1p_* notemptycheck_* negativecheck_* positivecheck_* acceleratie_* remmen_* bochten_*

* T3 (3n/0p)
local i=0

while `i'<=10 {
gen notemptycheck_`i'=cond(strpos(commentaar_in_`i', "Beste"),1,0) if treatment=="T4" & yesfeed_`i'==1 
gen acceleratie_`i'=cond(strpos(commentaar_in_`i', "accelereren"),1,0) if treatment=="T4" & yesfeed_`i'==1 & notemptycheck_`i'==1
gen bochten_`i'=cond(strpos(commentaar_in_`i', "bochten"),1,0) if treatment=="T4" & yesfeed_`i'==1 & notemptycheck_`i'==1
gen remmen_`i'=cond(strpos(commentaar_in_`i', "remmen"),1,0) if treatment=="T4" & yesfeed_`i'==1 & notemptycheck_`i'==1

replace ACCmsgNEG_`i'=acceleratie_`i' if treatment=="T4" & yesfeed_`i'==1 & notemptycheck_`i'==1
replace BOCHTmsgNEG_`i'=bochten_`i' if treatment=="T4" & yesfeed_`i'==1 & notemptycheck_`i'==1
replace REMmsgNEG_`i'=remmen_`i' if treatment=="T4" & yesfeed_`i'==1 & notemptycheck_`i'==1

local i=`i'+1
}

local i=0

while `i'<=10 {
gen feedback_3n0p_0n0p_`i'=cond(notemptycheck_`i'==0,1,0) if treatment=="T4" & yesfeed_`i'==1
qui sum feedback_3n0p_0n0p_`i'
local share_3n0p_0n0p_`i'=r(mean)
di `i'
di `share_3n0p_0n0p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
gen feedback_3n0p_3n0p_`i'=0 if treatment=="T4" & yesfeed_`i'==1
replace feedback_3n0p_3n0p_`i'=1 if acceleratie_`i'==1 & bochten_`i'==1 & remmen_`i'==1 
qui sum feedback_3n0p_3n0p_`i'
local share_3n0p_3n0p_`i'=r(mean)
di `i'
di `share_3n0p_3n0p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
gen feedback_3n0p_1n0p_`i'=0 if treatment=="T4" & yesfeed_`i'==1
replace feedback_3n0p_1n0p_`i'=1 if acceleratie_`i'==1 & bochten_`i'==0 & remmen_`i'==0 
replace feedback_3n0p_1n0p_`i'=1 if acceleratie_`i'==0 & bochten_`i'==1 & remmen_`i'==0 
replace feedback_3n0p_1n0p_`i'=1 if acceleratie_`i'==0 & bochten_`i'==0 & remmen_`i'==1 
qui sum feedback_3n0p_1n0p_`i'
local share_3n0p_1n0p_`i'=r(mean)
di `i'
di `share_3n0p_1n0p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
qui sum acceleratie_`i' if feedback_3n0p_1n0p_`i'==1
local acceleratie_3n0p_1n0p_`i'=r(mean)
qui sum remmen_`i' if feedback_3n0p_1n0p_`i'==1
local remmen_3n0p_1n0p_`i'=r(mean)
qui sum bochten_`i' if feedback_3n0p_1n0p_`i'==1
local bochten_3n0p_1n0p_`i'=r(mean)
di `i'
di `acceleratie_3n0p_1n0p_`i''
di `remmen_3n0p_1n0p_`i''
di `bochten_3n0p_1n0p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
gen feedback_3n0p_2n0p_`i'=0 if treatment=="T4" & yesfeed_`i'==1
replace feedback_3n0p_2n0p_`i'=1 if notemptycheck_`i'==1 & feedback_3n0p_1n0p_`i'==0  & feedback_3n0p_3n0p_`i'==0
qui sum feedback_3n0p_2n0p_`i'
local share_3n0p_2n0p_`i'=r(mean)
di `i'
di `share_3n0p_2n0p_`i''
local i=`i'+1
}

local i=0

while `i'<=10 {
qui sum acceleratie_`i' if feedback_3n0p_2n0p_`i'==1
local acceleratie_3n0p_2n0p_`i'=r(mean)
qui sum remmen_`i' if feedback_3n0p_2n0p_`i'==1
local remmen_3n0p_2n0p_`i'=r(mean)
qui sum bochten_`i' if feedback_3n0p_2n0p_`i'==1
local bochten_3n0p_2n0p_`i'=r(mean)
di `i'
di `acceleratie_3n0p_2n0p_`i''
di `remmen_3n0p_2n0p_`i''
di `bochten_3n0p_2n0p_`i''
local i=`i'+1
}

drop feedback_3n0p_0n0p_* feedback_3n0p_1n0p_* feedback_3n0p_2n0p_* feedback_3n0p_3n0p_* acceleratie_* bochten_* remmen_* notemptycheck_*


bysort interventie: sum REM*

*** 7. Save dataset for analysis. 

save "$filepath\ConstructedData\FeedBackReceived.dta", replace
